﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace BaseSyntax.Values.StringTest.Yaml.Utils
{
	public static class YamlUtils
	{
		// kv键值对
		static Regex kvReg = new Regex(@"^ *([a-zA-Z_]+): (.+?)[\r\n]+");
		// 空表
		static Regex emptyTableReg = new Regex(@"^ *(\w+): \[\][\r\n]*");
		// 空类型
		static Regex emptyReg = new Regex(@"^ *\w*: [\r\n]+");
		// 表
		static Regex tableReg = new Regex(@"^ *([a-zA-Z_]+): ({.*})[\n\r]*");
		// 数组
		static Regex arrReg = new Regex(@"^ *([a-zA-Z_]*):[\r\n]+(( *- [\w: ]*{.*}[\r\n]+)+)");
		// 复合类型
		static Match GetComplexMatch(string str1)
		{
			// 截取当前行的空格（作为层级标记
			string empty = Regex.Match(str1, @"^( +)").Value;
			// 下一级空格多两个，则说明格式有缩进
			Match match = Regex.Match(str1, $@"^{empty}[\w_]*:[\n\r]+{empty}  ");
			// 匹配失败，则直接返回此match
			if (!match.Success)
			{
				return match;
			}

			string head = @"^" + empty + @"([\w_]*):[\r\n]*";
			string content = "((" + empty + @"[  ]{1,}.+[\n\r]+)+)";
			match = Regex.Match(str1, head + content);
			
			return match;
		}

		static void KvResolver(Match m, string str1, Dictionary<string, object> parent)
		{
			string key = m.Groups[1].Value;
			string value = m.Groups[2].Value;
			
			parent[key] = value;
		}

		static void EmptyTableResolver(Match m, string str1, Dictionary<string, object> parent)
		{
			string key = m.Groups[1].Value;
			parent[key] = new Dictionary<string, object>();
		}

		static void EmptyResolver(Match m, string str1, Dictionary<string, object> parent)
		{
		}
		
		// talbe未处理嵌套
		static Dictionary<string, object> TableStrToTable(string tableStr)
		{
			Dictionary<string, object> dictionary = new Dictionary<string, object>();

			string pat = @"[ {]{1}(.*?): (.*?)[,}]{1}";

			var matchs = Regex.Matches(tableStr, pat);
			foreach (Match match in matchs)
			{
				string key1 = match.Groups[1].Value;
				string value1 = match.Groups[2].Value;
				dictionary[key1] = value1;
			}

			return dictionary;
		}

		static void TableResolver(Match m, string str1, Dictionary<string, object> parent)
		{
			string key = m.Groups[1].Value;
			string value = m.Groups[2].Value;

			parent[key] = TableStrToTable(value);
		}

		// 目前只处理了子组为table的类型
		static void ArrayResolver(Match m, string str1, Dictionary<string, object> parent)
		{
			string key = m.Groups[1].Value;

			ArrayList arrayList = new ArrayList();

			string arrayStr = m.Groups[2].Value;

			string pat = @" *- [\w: ]*({.+})[\n\r]*";

			foreach (Match match in Regex.Matches(arrayStr, pat))
			{
				string tableStr = match.Groups[1].Value;
				arrayList.Add(TableStrToTable(tableStr));
			}

			parent[key] = arrayList;
		}

		static void CutStr(string str1, Dictionary<string, object> parent)
		{
			Match complexMatch = GetComplexMatch(str1);
			Match kvMatch = kvReg.Match(str1);
			Match tableMatch = tableReg.Match(str1);
			Match emptyTabMatch = emptyTableReg.Match(str1);
			Match arrMatch = arrReg.Match(str1);
			Match emptyMatch = emptyReg.Match(str1);

			Match m = null;
			// 这边其实可以为 if else 结构
			if (kvMatch.Success)
			{
				KvResolver(kvMatch, str1, parent);
				m = kvMatch;
			}

			if (emptyTabMatch.Success)
			{
				EmptyTableResolver(emptyTabMatch, str1, parent);
				m = emptyTabMatch;
			}

			if (emptyMatch.Success)
			{
				EmptyResolver(m, str1, parent);
				m = emptyMatch;
			}

			if (tableMatch.Success)
			{
				TableResolver(tableMatch, str1, parent);
				m = tableMatch;
			}

			if (arrMatch.Success)
			{
				ArrayResolver(arrMatch, str1, parent);
				m = arrMatch;
			}

			if (complexMatch.Success)
			{
				Dictionary<string, object> d2 = new Dictionary<string, object>();

				string name = complexMatch.Groups[1].Value;
				parent[name] = d2;
				CutStr(complexMatch.Groups[2].Value, d2);
				m = complexMatch;
			}

			if (m != null)
			{
				string txt = m.Groups[0].Value;
				// 删掉被解析掉的头
				str1 = str1.Replace(txt, "");

				if (str1 != "")
				{
					CutStr(str1, parent);
				}
			}
		}

		public static Dictionary<string, object> ToTable()
		{
			var dAll = new Dictionary<string, object>();
		
			string data = FileUtils.ReadUtf8File(EnvUtils.GetAssetPath() + "/Prefabs/Corona.prefab");
			data = Regex.Replace(data, @"%YAML 1.1[\n\r]*%TAG !u! tag:unity3d.com,2011:[\r\n]*", "");
			// 根据--- 切割单独的对象
			string[] st1 = {"---"};
			string[] splitData = data.Split(st1, StringSplitOptions.None);
	
			foreach (string subStr in splitData)
			{
				if (subStr == "")
				{
					continue;
				}
				else
				{
					var d1 =new Dictionary<string, object>();
					string str = subStr;
					
					Match m1 = Regex.Match(str, @" *!u!(\d*) &(\d*)[\r\n]*");
					string fileId = m1.Groups[2].Value;
					d1["classId"] = m1.Groups[1].Value;
					d1["FileId"] = fileId;
					
					dAll[fileId] = d1;
					// 切掉头部
					str = Regex.Replace(str, m1.Groups[0].Value, "");
					
					// 切掉类型字符
					m1 = Regex.Match(str, @"(\w*):[\r\n]*");
					d1["UType"] = m1.Groups[1].Value;
					
					str = Regex.Replace(str, m1.Groups[0].Value, "");
					
					CutStr(str, d1);
				}
			}

			return dAll;
		}
	}
}